Skip to content

Add convenience helpers for conditions, status, and resource names#205

Open
negz wants to merge 5 commits into
crossplane:mainfrom
negz:helpers
Open

Add convenience helpers for conditions, status, and resource names#205
negz wants to merge 5 commits into
crossplane:mainfrom
negz:helpers

Conversation

@negz
Copy link
Copy Markdown
Member

@negz negz commented May 21, 2026

Description of your changes

A few small convenience helpers I've wanted across several composition functions I'm working on.

  • response.set_condition accepts a resource.Condition — the same type returned by get_condition — and appends the corresponding fnv1.Condition to the response. The SDK had read support for conditions via get_condition but no write support; functions had to build fnv1.Condition protobuf messages by hand.
  • resource.child_name builds deterministic, DNS-label-safe names for child resources. Composition functions that derive child names from parent names hit the 63-character DNS label limit; this encodes the truncate-and-hash pattern once.
  • resource.update_status nests a dict or Pydantic model under the status key and calls resource.update. Nearly every composition function writes status, and the wrapping is universal boilerplate.

I have:

negz added 4 commits May 20, 2026 18:51
The SDK has resource.get_condition and resource.Condition for reading
conditions from observed resources, but no way to write conditions back
to the response. Functions build fnv1.Condition protobuf messages by
hand, mapping status strings to protobuf enums and setting the target
each time.

This commit adds response.set_condition, which accepts a
resource.Condition — the same type returned by get_condition — and
appends the corresponding fnv1.Condition to the response.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Composition functions often derive child resource names by combining a
parent name with a discriminator. The resulting name must be a valid DNS
label — at most 63 characters. Names built from user-supplied components
can exceed this limit, and functions end up writing ad-hoc truncation
logic.

This commit adds resource.child_name, which joins name parts, appends a
deterministic 5-character SHA-256 hash suffix for uniqueness, and
truncates the prefix to fit within the 63-character limit.

    child_name("my-xr", "bucket")
    # "my-xr-bucket-05ecb"

    child_name("my-very-long-deployment", "us-central1-a-gpu-cluster")
    # "my-very-long-deployment-us-central1-a-gpu-cluste-7e130"

Signed-off-by: Nic Cope <nicc@rk0n.org>
Functions that set status on the composite resource have to wrap their
status in {"status": ...} and call resource.update. This is a small but
universal bit of boilerplate — nearly every composition function writes
status.

This commit adds resource.update_status, which accepts a dict or
Pydantic model and nests it under the status key.

Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit renames set_condition to set_conditions and changes the
signature to accept variadic resource.Condition arguments. Functions
that set multiple conditions can now pass them all in one call.

Signed-off-by: Nic Cope <nicc@rk0n.org>
@negz negz marked this pull request as ready for review May 21, 2026 02:13
@negz negz requested a review from bobh66 as a code owner May 21, 2026 02:13
Copy link
Copy Markdown
Member

@ezgidemirel ezgidemirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @negz!

When message is empty, omitting the field from the protobuf message
is cleaner than explicitly setting it to an empty string. Protobuf
includes explicitly-set zero values in serialization, so setting
message="" causes it to appear in MessageToDict output and gRPC
responses even when there is nothing to say.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants